/**
 * Redis客户端
 * 目前只实现 get、set和incr操作
 */
public class MyJedis {
	MySocket socket = new MySocket("127.0.0.1",6379);
	public String set(String key, String value) {
		socket.send(
				commandUtil(Resp.command.SET,key.getBytes(),value.getBytes()));
		return socket.read();
	}
	public String get(String key) {
		socket.send(
				commandUtil(Resp.command.GET,key.getBytes()));
		return socket.read();
	}
	public String incr(String key) {
		socket.send(
				commandUtil(Resp.command.INCR,key.getBytes()));
		return socket.read();
	}

	//命令装换，将传入参数转化成redis指令字符串
	public static String commandUtil(Resp.command command, byte[]...bytes) {
		StringBuilder stringBuilder = new StringBuilder();
		//指令组数
		stringBuilder.append(Resp.star)
				.append(1+bytes.length)
				.append(Resp.line);
		//这个指令的字符数
		stringBuilder.append(Resp.stringLength)
				.append(command.toString().length())
				.append(Resp.line);
		//表示某个指令的字符串
		stringBuilder.append(command.toString())
				.append(Resp.line);
		//循环获取指令参数
		for(byte[] aByte : bytes) {
			stringBuilder.append(Resp.stringLength)
					.append(aByte.length)
					.append(Resp.line);
			stringBuilder.append(new String(aByte))
					.append(Resp.line);
		}

		return stringBuilder.toString();

	}


}
